package com.betterwood.dragon.dao.enhances.impl;

import com.betterwood.dragon.dao.common.MongoDBUtils;
import com.betterwood.dragon.dao.enhances.CustomeProductRepo;
import com.betterwood.dragon.dao.enhances.CustomeRecordRepo;
import com.betterwood.dragon.dao.enhances.UserMongoRepository;
import com.betterwood.dragon.entity.Product;
import com.betterwood.dragon.entity.Record;
import com.betterwood.dragon.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import jdk.internal.jline.internal.Log;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.regex.Pattern;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * @author zhengyanhua
 * @create 2021-03-18 13:59
 */
@Slf4j
public class CustomeProductRepoImpl implements CustomeProductRepo {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Product> listByQuery(Product product) {
        Query query = new Query();
        if (StrUtil.isNotBlank(product.getSiteNo())) {
            log.info("FFFFFFF");
            log.info(product.getSiteNo());
            query.addCriteria(where("siteNo").is(product.getSiteNo()));
        }

        if (ObjectUtil.isNotNull(product.getStatus())) {
            log.info("BBBBBBBBB");

            query.addCriteria(where("status").is(product.getStatus()));
        }

        if (StrUtil.isNotBlank(product.getSkuName()) && !StrUtil.equals(product.getSkuName(), "ALL")) {
            log.info("HHHHHHHHHH");
            log.info(product.getSkuName());
            Pattern pattern = Pattern.compile("^.*" + product.getSkuName() + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(where("skuName").regex(pattern));
        }

        return mongoTemplate.find(query, Product.class);
    }
}
